package com.wehealth.electrocardiograph_two_client.activity

import android.Manifest
import android.content.Intent
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.view.KeyEvent
import com.wehealth.electrocardiograph_two_client.R
 import com.wehealth.electrocardiograph_two_client.databinding.ActivityMainBinding
import com.wehealth.electrocardiograph_two_client.presenter.MainPresenter
import com.wehealth.electrocardiograph_two_client.utils.FileUtils
import com.wehealth.three.lib_common.manager.AppManager
import com.wehealth.three.lib_common.ui.component.BaseActivity
import com.wehealth.three.lib_common.utils.PermissionUtils
import com.wehealth.three.lib_common.utils.isHarmony
import com.wehealth.three.lib_connect.constant.ConstantKey.adb_command_dir
import com.wehealth.three.lib_connect.constant.ConstantKey.adb_command_typec


class MainActivity : BaseActivity<ActivityMainBinding>(
    darkContent = false,
    translucent = true
) {
    private val permissions = arrayListOf<String>()
    lateinit var mainPresenter: MainPresenter
    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initData() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                startActivity(intent)
                return
            }

        }
        requestPermission()
        mainPresenter = MainPresenter(MainActivity@ this, dataBinding)
        dataBinding.mainPresenter = mainPresenter
        mainPresenter.showPrivateDialog()

        dataBinding.mainGenderRg.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.main_gender_male -> {
                    dataBinding.mainGenderMale.isChecked = true
                }

                R.id.main_gender_female -> {
                    dataBinding.mainGenderFemale.isChecked = true
                }
            }
        }
    }

    /***
     * 处理蓝牙权限申请问题
     */
    private fun requestPermission() {

        when {
            Build.VERSION.SDK_INT > Build.VERSION_CODES.R -> {
                permissions.add(Manifest.permission.BLUETOOTH_SCAN)
                permissions.add(Manifest.permission.BLUETOOTH_CONNECT)
                permissions.add(Manifest.permission.CAMERA)
                permissions.add(Manifest.permission.RECORD_AUDIO)
            }

            isHarmony(MainActivity@ this) -> {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION)
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION)
                permissions.add(Manifest.permission.CAMERA)
                permissions.add(Manifest.permission.RECORD_AUDIO)
            }

            else -> {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION)
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION)
                permissions.add(Manifest.permission.CAMERA)
                permissions.add(Manifest.permission.RECORD_AUDIO)
            }
        }
        PermissionUtils.requestPermission(
            MainActivity@ this,
            permissions
        ) { allGranted, _, _ ->
            if (allGranted) {

            }
        }

    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            AppManager.getAppManager().finishAllActivity()
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        FileUtils.writeContentToFile(adb_command_dir, adb_command_typec.toCharArray())

    }

}